#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* ps)
{
	assert(ps);
	SLDataType* tmp = (SLDataType*)malloc(sizeof(SLDataType)*InitCapacity);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->a = tmp;
	ps->size = 0;
	ps->capacity = InitCapacity;
	
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
	
}
void SLPrint(SL* ps)
{
	int i = 0;
	for (i = 0;i < ps->size;i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SLPushFront(SL* ps, SLDataType x)
{
	/*assert(ps);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;*/
	SLInsert(ps, 0, x);
}
void SLPopFront(SL* ps)
{
	/*assert(ps->size > 0);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;*/
	SLErase(ps, 0);
}
void SLPushBack(SL* ps, SLDataType x)
{
	/*assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size++] = x;
	*/
	SLInsert(ps, ps->size , x);

}

void SLPopBack(SL* ps)
{
	/*assert(ps->size > 0);
	ps->size--;*/
	SLErase(ps, ps->size - 1);
}
void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity) * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;

	}
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (pos<=end)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
	

}
void SLErase(SL* ps, int pos)
{

	assert(ps);
	assert(ps->size > 0&&pos<ps->size);
	while (pos < ps->size-1)
	{
		ps->a[pos++] = ps->a[pos + 1];
	}
	ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	int i = 0;
	for (i = 0;i < ps->size;i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}